<template>
  <div class="food-card-page">
    <el-card
      class="aui-card--fill"
      shadow="never">
      <el-form inline>
        <!--                      <el-form-item prop="cityId">-->
        <!--                        &lt;!&ndash;                            v-model="dataForm.cityId"&ndash;&gt;-->
        <!--                        <el-select clearable placeholder="城市">-->
        <!--                          <el-option-->
        <!--                            :key="'city-' + city.id"-->
        <!--                            :label="city.name"-->
        <!--                            :value="city.id"-->
        <!--                            v-for="city in cities"-->
        <!--                          ></el-option>-->
        <!--                        </el-select>-->
        <!--                      </el-form-item>-->
        <el-form-item>
          <el-button
            @click="clickOnAddOrUpdate()"
            type="primary">
            <i class="el-icon-plus"/>
            <span>新增</span>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="table-container">
        <el-table
          :data="dataList"
          border
          style="width: 100%;"
          v-loading="dataListLoading"
        >
          <!--                    <el-table-column label="ID" prop="id"></el-table-column>-->
          <el-table-column
            label="图片"
            prop="url"
            width="120">
            <template slot-scope="scope">
              <midea-admin-image
                :height="100"
                :previewable="true"
                :url="scope.row.url"
                :width="100"
              />
            </template>
          </el-table-column>

          <el-table-column
            label="城市"
            prop="cityId">
            <template slot-scope="scope">
              {{ getCardCityName(scope.row.cityId) }}
            </template>
          </el-table-column>

          <el-table-column
            label="美食故事"
            prop="foodStoryId">
            <template slot-scope="scope">
              {{ getCardFoodStoryName(scope.row.foodStoryId) }}
            </template>
          </el-table-column>
          <!--          <el-table-column label="城市" prop="cityUrl"></el-table-column>-->
          <el-table-column
            :label="$t('handle')"
            align="center"
            fixed="right"
            header-align="center"
            width="160"
          >
            <template slot-scope="scope">
              <el-button
                @click="clickOnAddOrUpdate(scope.row.id)"
                plain
                size="small"
                type="primary"
              >
                {{ $t("update") }}
              </el-button>
              <el-button
                @click="deleteHandle(scope.row.id)"
                plain
                size="small"
                type="danger"
              >{{ $t("delete") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-size="limit"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          @current-change="pageCurrentChangeHandle"
          @size-change="pageSizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
      <!-- 弹窗, 新增 / 修改 -->

      <foodcard-add-or-update
        @refreshDataList="getDataList"
        ref="addOrUpdate"
        v-if="addOrUpdateVisible"
      />

      <!--            <citystory-add-or-update-->
      <!--                    @refreshDataList="getDataList"-->
      <!--                    ref="addOrUpdate"-->
      <!--                    v-if="addOrUpdateVisible"-->
      <!--            ></citystory-add-or-update>-->
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/mike.view-module'
import FoodcardAddOrUpdate from './foodcard-add-or-update'
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'FoodCardPage',
  components: { FoodcardAddOrUpdate },
  mixins: [mixinViewModule],
  data () {
    return {
      cities: [],
      foodStories: [],
      mixinViewModuleOptions: {
        getDataListURL: '/midea/menu/foodmap/foodcards/getallfoodcards',
        getDataListIsPage: true,
        deleteURL: '/midea/menu/foodmap/foodcards/del/'
        // deleteIsBatch: true
      },
      dataForm: {
        // cityId: ''
      }
    }
  },
  mounted () {
    this.getDataList()
    this.getAllFoodStory()

    this.getCities()
  },
  methods: {
    getCities () {
      this.$http
        .post(`${this.mikeNetworkBase}midea/menu/foodmap/common/citys/`)
        .then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }
          this.cities = res.data
        })
    },
    getAllFoodStory () {
      this.$http
        .post(`${this.mikeNetworkBase}midea/menu/foodmap/foodstorys/getall`, {
          pageSize: 1000,
          currentPage: 1
        })
        .then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }
          console.log(res.data)
          this.foodStories = res.data
        })
    },
    getCardCityName (cityId) {
      let model = this.cities.find(item => {
        return item.id === cityId
      })
      return model ? model.name : ''
    },
    getCardFoodStoryName (foodStoryId) {
      let model = this.foodStories.find(item => {
        return item.id === foodStoryId
      })
      return model ? model.title : ''
    },
    clickOnAddOrUpdate (id) {
      this.addOrUpdateVisible = true

      let model = this.dataList.find(item => {
        return item.id === id
      })
      this.$nextTick(() => {
        this.$refs.addOrUpdate.reset()
        if (model) {
          // model
          this.$refs.addOrUpdate.form = cloneDeep(model)
        }

        this.$refs.addOrUpdate.init()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.food-card-page {
}
</style>
